//
//  SystemMsgCell.swift
//  d11
//
//  Created by peale on 2018/7/31.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit

class SystemMsgCell: UITableViewCell {
    var  bgView : UIImageView?
    var titleLabel : UILabel?
    var timeLabel : UILabel?
    var xlineView : UIImageView?
    var contentLabel : UILabel?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.multipleSelectionBackgroundView = UIView.init()
        self.tintColor = UIColor.orange
        self.setUpUI()

    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
       // self.selectionStyle = .none
        self.bgView = UIImageView.init()
        self.bgView?.image = imageNamed("icon_my_system_msg_cell_bg")
        self.contentView.addSubview(self.bgView!)
        
        self.bgView?.snp.makeConstraints{(make) in
            make.left.equalTo(5)
            make.right.equalTo(-5)
            make.height.equalTo(130)
        }
        
        self.titleLabel = UILabel.init()
        self.titleLabel?.textColor = HexRGBAlpha(0x333333, 1)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.titleLabel?.textAlignment = NSTextAlignment.left
        self.bgView?.addSubview(self.titleLabel!)
        
        self.titleLabel?.snp.makeConstraints{(make) in
            make.left.equalTo(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(15)
            make.top.equalTo(20)
        }
        
        
        self.timeLabel = UILabel.init()
        self.timeLabel?.textColor = HexRGBAlpha(0x666666, 1)
        self.timeLabel?.font = UIFont.systemFont(ofSize: 13)
        self.timeLabel?.textAlignment = NSTextAlignment.left
        self.bgView?.addSubview(self.timeLabel!)
        
        self.timeLabel?.snp.makeConstraints{(make) in
            make.left.equalTo(20)
            make.top.equalTo((titleLabel?.snp.bottom)!).offset(5)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(15)
        }
        
        self.xlineView = UIImageView.init()
        self.xlineView?.image =  UIImage.imageWithColor(color: HexRGBAlpha(0xdddddd, 1), size: CGSize.init(width: kScreenWidth, height: 1))  // HexRGBAlpha(0xdddddd, 1)
        self.bgView?.addSubview(self.xlineView!)
        
        self.xlineView?.snp.makeConstraints{(make) in
            make.top.equalTo((self.timeLabel?.snp.bottom)!).offset(5)
            make.left.equalToSuperview().offset(7.5)
            make.height.equalTo(0.5)
            make.right.equalToSuperview().offset(-7.5)
        }
        
        
        self.contentLabel = UILabel.init()
        self.contentLabel?.textColor = HexRGBAlpha(0x666666, 1)
        self.contentLabel?.font = UIFont.systemFont(ofSize: 14)
        self.contentLabel?.textAlignment = NSTextAlignment.left
        self.contentLabel?.numberOfLines = 3
        self.bgView?.addSubview(self.contentLabel!)
        
        self.contentLabel?.snp.makeConstraints{(make) in
            make.left.equalTo(20)
            make.top.equalTo((xlineView?.snp.bottom)!)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(55)
        }
    }
    
    func setData(dict : [String:Any]) -> Void {
        self.titleLabel?.text = dict["title"] as? String
        self.timeLabel?.text =  dict["created"] as? String
        self.contentLabel?.text = (dict["content"] as? String)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
                for control in self.subviews{
                    if control.isMember(of: NSClassFromString("UITableViewCellEditControl")!){
                        for v in control.subviews{
                            if(v.isKind(of:UIImageView.self)){
                                let img = v as! UIImageView
                                if(self.isSelected){
                                    img.image = UIImage.init(named: "msg_cell_edit_s")
                                }else{
                                    img.image = UIImage.init(named: "msg_cell_edit_n")
                                }
                            }
                        }
                    }
        }
    }
    
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        for control in self.subviews{
        if control.isMember(of: NSClassFromString("UITableViewCellEditControl")!){
            for v in control.subviews{
                if(v.isKind(of:UIImageView.self)){
                    let img = v as! UIImageView
                    if(!self.isSelected){
                        img.image = UIImage.init(named: "msg_cell_edit_n")
                    }
                }
            }
        }
    }
    }
        
}

